package kr.co.bettersoft.waitnumbercustomer.adapter;

import java.util.List;

import kr.co.bettersoft.waitnumbercustomer.activity.R;
import kr.co.bettersoft.waitnumbercustomer.common.CommonConstant;
import kr.co.bettersoft.waitnumbercustomer.domain.Merchant;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class MemberStoreListAdapter extends BaseAdapter {
	String TAG = "MemberStoreListAdapter";
	private Context context;
	private final List<Merchant> entries;
	String tmpStr = "";
	
	// 폰트
	private Typeface typefaceNanumGothic= null; 
	private Typeface typefaceNanumGothicBold = null;
	
	public MemberStoreListAdapter(Context context, List<Merchant> entriesFn) {		
		this.context = context;
		this.entries = entriesFn;
		
		// 폰트 지정.
	    typefaceNanumGothic = Typeface.createFromAsset(context.getAssets(), CommonConstant.NanumGothic);  
	    typefaceNanumGothicBold = Typeface.createFromAsset(context.getAssets(), CommonConstant.NanumGothicBold);
	}
 
	public View getView(int position, View convertView, ViewGroup parent) {
 
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
		View listView;		
 
		if (convertView == null) {
 
			listView = new View(context);
 
			// get layout from mobile.xml
			listView = inflater.inflate(R.layout.member_store_list_adapter, null);
 
			// ** 이 소스를 여기에 두면 한 화면의 이미지가 반복 된다. 아래쪽 return 위로 옮겨주면 이미지 반복 현상이 없다. 
//			// set value into textview
//			TextView textView = (TextView) listView
//					.findViewById(R.id.label);
//			textView.setText(((Merchant)entries.get(position)).getCompanyName());
//			// set image based on selected text
//			ImageView imageView = (ImageView) listView
//					.findViewById(R.id.icon);
////			String mobile = entries[position];
//			imageView.setImageBitmap(((Merchant)entries.get(position)).getMerchantImage());
		} else {
			listView = (View) convertView;
		}
 
		// ** 이미지 반복 현상을 없애기 위해 위쪽의 소스를 여기로 옮겨준다. --> 문제 해결 됨.
		// set value into textview
		TextView textView = (TextView) listView.findViewById(R.id.label);
		ImageView imageView = (ImageView) listView.findViewById(R.id.icon);
		RatingBar mRating = (RatingBar) listView.findViewById(R.id.rating_bar);
		TextView evalValueView = (TextView) listView.findViewById(R.id.eval_value);
		TextView evalTotalView = (TextView) listView.findViewById(R.id.eval_total);
		
		
		textView.setText(((Merchant)entries.get(position)).getCompanyName());

		// 먼저 받은 인원수 flaot 문자열 값을 소수로 변환하고, int 로 바꿔서 소수점을 뺀 다음 다시 문자열로 바꿈. 그리고 앞뒤에 (,  명) 을 붙임  
		tmpStr = (Integer.toString(((int)(Float.parseFloat(((Merchant)entries.get(position)).getEvaluationCount())))))+context.getString(R.string.companion_after_text);
		evalTotalView.setText(tmpStr);
		evalValueView.setText(((Merchant)entries.get(position)).getEvaluationValue());  
		
		// set image based on selected text
		
//		String mobile = entries[position];
		imageView.setImageBitmap(((Merchant)entries.get(position)).getMerchantImageBmp());
		
		// 별점
		tmpStr = ((Merchant)entries.get(position)).getEvaluationValue();
		if(tmpStr==null || tmpStr.length()<1){
			tmpStr = "0.0";
		}
		mRating.setRating(Float.parseFloat(tmpStr)/2);
		
		// 폰트 적용
		textView.setTypeface(typefaceNanumGothicBold);		// 상호명
		evalTotalView.setTypeface(typefaceNanumGothic);
		evalValueView.setTypeface(typefaceNanumGothic);
		
		return listView;
	}
 
	@Override
	public int getCount() {
		return entries.size();
	}
 
	@Override
	public Object getItem(int position) {
		return null;
	}
 
	@Override
	public long getItemId(int position) {
		return 0;
	}
 
}